home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 6 / adb / s-wchcnv < prev    next >
Text File  |  1996-02-12  |  9KB  |  257 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ C N V                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.4 $                              --
  10. --                                                                          --
  11. --   Copyright (C) 1992,1993,1994,1995,1996 Free Software Foundation, Inc.  --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. --  This package contains generic subprograms used for converting between
  37. --  sequences of Character and Wide_Character. All access to wide character
  38. --  sequences is isolated in this unit.
  39.  
  40. with System.WCh_Con; use System.WCh_Con;
  41. with System.WCh_JIS; use System.WCh_JIS;
  42.  
  43. package body System.WCh_Cnv is
  44.  
  45.    --------------------------------
  46.    -- Char_Sequence_To_Wide_Char --
  47.    --------------------------------
  48.  
  49.    function Char_Sequence_To_Wide_Char
  50.      (C    : Character;
  51.       EM   : WC_Encoding_Method)
  52.       return Wide_Character
  53.    is
  54.       B1 : Integer;
  55.       C1 : Character;
  56.  
  57.       procedure Get_Hex (N : Character);
  58.       --  If N is a hex character, then set B1 to 16 * B1 + character N.
  59.       --  Raise Constraint_Error if character N is not a hex character.
  60.  
  61.       -------------
  62.       -- Get_Hex --
  63.       -------------
  64.  
  65.       procedure Get_Hex (N : Character) is
  66.          B2 : constant Integer := Character'Pos (N);
  67.  
  68.       begin
  69.          if B2 in Character'Pos ('0') .. Character'Pos ('9') then
  70.             B1 := B1 * 16 + B2 - Character'Pos ('0');
  71.  
  72.          elsif B2 in Character'Pos ('A') .. Character'Pos ('F') then
  73.             B1 := B1 * 16 + B2 - (Character'Pos ('A') - 10);
  74.  
  75.          elsif B2 in Character'Pos ('a') .. Character'Pos ('f') then
  76.             B1 := B1 * 16 + B2 - (Character'Pos ('a') - 10);
  77.  
  78.          else
  79.             raise Constraint_Error;
  80.          end if;
  81.       end Get_Hex;
  82.  
  83.    --  Start of processing for Char_Sequence_To_Wide_Char
  84.  
  85.    begin
  86.       case EM is
  87.  
  88.          when WCEM_None | WCEM_Brackets =>
  89.  
  90.             if C /= '[' then
  91.                return Wide_Character'Val (Character'Pos (C));
  92.             end if;
  93.  
  94.             if In_Char /= '"' then
  95.                raise Constraint_Error;
  96.             end if;
  97.  
  98.             B1 := 0;
  99.             Get_Hex (In_Char);
  100.             Get_Hex (In_Char);
  101.             C1 := In_Char;
  102.  
  103.             if C1 /= '"' then
  104.                Get_Hex (C1);
  105.                Get_Hex (In_Char);
  106.                C1 := In_Char;
  107.  
  108.                if C1 /= '"' then
  109.                   raise Constraint_Error;
  110.                end if;
  111.             end if;
  112.  
  113.             if In_Char /= ']' then
  114.                raise Constraint_Error;
  115.             end if;
  116.  
  117.             return Wide_Character'Val (B1);
  118.  
  119.          when WCEM_Hex =>
  120.             if C /= Ascii.ESC then
  121.                return Wide_Character'Val (Character'Pos (C));
  122.  
  123.             else
  124.                B1 := 0;
  125.                Get_Hex (In_Char);
  126.                Get_Hex (In_Char);
  127.                Get_Hex (In_Char);
  128.                Get_Hex (In_Char);
  129.  
  130.                return Wide_Character'Val (B1);
  131.             end if;
  132.  
  133.          when WCEM_Upper =>
  134.             if C > Ascii.DEL then
  135.                return
  136.                  Wide_Character'Val
  137.                    (Integer (256 * Character'Pos (C)) +
  138.                     Character'Pos (In_Char));
  139.             else
  140.                return Wide_Character'Val (Character'Pos (C));
  141.             end if;
  142.  
  143.          when WCEM_Shift_JIS =>
  144.             if C > Ascii.DEL then
  145.                return Shift_JIS_To_JIS (C, In_Char);
  146.             else
  147.                return Wide_Character'Val (Character'Pos (C));
  148.             end if;
  149.  
  150.          when WCEM_EUC =>
  151.             if C > Ascii.DEL then
  152.                return EUC_To_JIS (C, In_Char);
  153.             else
  154.                return Wide_Character'Val (Character'Pos (C));
  155.             end if;
  156.  
  157.       end case;
  158.    end Char_Sequence_To_Wide_Char;
  159.  
  160.    --------------------------------
  161.    -- Wide_Char_To_Char_Sequence --
  162.    --------------------------------
  163.  
  164.    procedure Wide_Char_To_Char_Sequence
  165.      (WC : Wide_Character;
  166.       EM : WC_Encoding_Method)
  167.    is
  168.       Val    : constant Natural := Wide_Character'Pos (WC);
  169.       Hexc   : constant array (0 .. 15) of Character := "0123456789ABCDEF";
  170.       C1, C2 : Character;
  171.  
  172.    begin
  173.       case EM is
  174.  
  175.          when WCEM_None =>
  176.  
  177.             if Val < 256 then
  178.                Out_Char (Character'Val (Val));
  179.  
  180.             else
  181.                Out_Char ('[');
  182.                Out_Char ('"');
  183.                Out_Char (Hexc (Val / (16**3)));
  184.                Out_Char (Hexc ((Val / (16**2)) mod 16));
  185.                Out_Char (Hexc ((Val / 16) mod 16));
  186.                Out_Char (Hexc (Val mod 16));
  187.                Out_Char ('"');
  188.                Out_Char (']');
  189.             end if;
  190.  
  191.          when WCEM_Hex =>
  192.             if Val < 256 then
  193.                Out_Char (Character'Val (Val));
  194.  
  195.             else
  196.                Out_Char (Ascii.ESC);
  197.                Out_Char (Hexc (Val / (16**3)));
  198.                Out_Char (Hexc ((Val / (16**2)) mod 16));
  199.                Out_Char (Hexc ((Val / 16) mod 16));
  200.                Out_Char (Hexc (Val mod 16));
  201.             end if;
  202.  
  203.          when WCEM_Upper =>
  204.             if Val < 128 then
  205.                Out_Char (Character'Val (Val));
  206.  
  207.             elsif Val < 16#8000# then
  208.                raise Constraint_Error;
  209.  
  210.             else
  211.                Out_Char (Character'Val (Val / 256));
  212.                Out_Char (Character'Val (Val mod 256));
  213.             end if;
  214.  
  215.          when WCEM_Shift_JIS =>
  216.             if Val < 128 then
  217.                Out_Char (Character'Val (Val));
  218.             else
  219.                JIS_To_Shift_JIS (WC, C1, C2);
  220.                Out_Char (C1);
  221.                Out_Char (C2);
  222.             end if;
  223.  
  224.          when WCEM_EUC =>
  225.             if Val < 128 then
  226.                Out_Char (Character'Val (Val));
  227.             else
  228.                JIS_To_EUC (WC, C1, C2);
  229.                Out_Char (C1);
  230.                Out_Char (C2);
  231.             end if;
  232.  
  233.          when WCEM_Brackets =>
  234.  
  235.             if Val < 128 then
  236.                Out_Char (Character'Val (Val));
  237.  
  238.             else
  239.                Out_Char ('[');
  240.                Out_Char ('"');
  241.  
  242.                if Val >= 256 then
  243.                   Out_Char (Hexc (Val / (16**3)));
  244.                   Out_Char (Hexc ((Val / (16**2)) mod 16));
  245.                end if;
  246.  
  247.                Out_Char (Hexc ((Val / 16) mod 16));
  248.                Out_Char (Hexc (Val mod 16));
  249.                Out_Char ('"');
  250.                Out_Char (']');
  251.             end if;
  252.  
  253.       end case;
  254.    end Wide_Char_To_Char_Sequence;
  255.  
  256. end System.WCh_Cnv;
  257.